package com.lanou.myapp.funny.picturefunny;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.GsonRequest;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by dllo on 16/1/12.
 */
public class PictureFragment extends Fragment {
    private String url = "http://s.budejie.com/topic/list/jingxuan/10/budejie-android-6.2.7/0-20.json?market=wandoujia&ver=6.2.7&visiting=&os=5.1&appname=baisibudejie&client=android&udid=000000000000000&mac=08%3A00%3A27%3A3f%3A7a%3A01";
    private Context context;
    private RecyclerView recyclerView;
    private PictureAdapter adapter;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView_picture_funny);
        adapter = new PictureAdapter(getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //如果确定每个item的内容不会改变RecyclerView的大小，设置这个选项可以提高性能
//        recyclerView.setHasFixedSize(true);

        GsonRequest<PictureFunnyBeans> request = new GsonRequest<PictureFunnyBeans>(url, PictureFunnyBeans.class, new Response.Listener<PictureFunnyBeans>() {
            @Override
            public void onResponse(PictureFunnyBeans response) {
                adapter.addData(response.getList());
                recyclerView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance()._addRequest(request);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_picture_funny, null);
        return view;
    }
}
